package com.yunfeisoft.base.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.model.WorkflowHistory;
import com.yunfeisoft.base.service.inter.WorkflowHistoryService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.utils.ApiUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: WorkflowHistoryController
 * Description: 流程实例执行历史Controller
 * Author: Jackie liu
 * Date: 2020-01-03
 */
@RestController
public class WorkflowHistoryController extends BaseController {

    @Autowired
    private WorkflowHistoryService workflowHistoryService;

    /**
     * 分页查询流程实例执行历史
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowHistory/list", method = {RequestMethod.POST, RequestMethod.GET})
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String title = ServletRequestUtils.getStringParameter(request, "title", null);
        String instanceCreateName = ServletRequestUtils.getStringParameter(request, "instanceCreateName", null);
        int status = ServletRequestUtils.getIntParameter(request, "status", -1);

        User loginUser = ApiUtils.getLoginUser();

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("title", title);
        params.put("instanceCreateName", instanceCreateName);
        params.put("executor", loginUser.getId());
        if (status > 0) {
            params.put("status", status);
        }
        Page<WorkflowHistory> page = workflowHistoryService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 查询流程实例执行历史
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowHistory/queryList", method = {RequestMethod.POST, RequestMethod.GET})
    public Response queryList(HttpServletRequest request, HttpServletResponse response) {
        String formId = ServletRequestUtils.getStringParameter(request, "formId", "no_");
        if (StringUtils.isBlank(formId)) {
            formId = "no_";
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("formId", formId);

        List<WorkflowHistory> list = workflowHistoryService.queryList(params);
        return ResponseUtils.success(list);
    }

}
